import * as Cesium from 'cesium';
import * as Libora from '../../public/libora/index.js'
import { EntityLib } from '../entity/EntityLib.js'
import { HelperLib } from './HelperLib.js'

(window).Cesium = Cesium;
(window).Libora = Libora;

let info = {
  // lon: 116.39713829145771,
  // lat : 39.91941703971191,
  lon: 0,
  lat : 0,
};


export default class MeshPlane {
  constructor() {
    const viewer = Libora.getViewer({
      // imageryProvider: false,
      // globe: false,
    });
    viewer.scene.globe.translucency.enabled = true
    viewer.scene.globe.baseColor = Cesium.Color.TRANSPARENT;
    viewer.scene.globe.showGroundAtmosphere = false
    viewer.scene.globe.undergroundColor = undefined;
    viewer.camera.flyTo({
      destination: Cesium.Cartesian3.fromDegrees(info.lon, info.lat, 1000000.0),
      duration: 0,
    });

    const position = Cesium.Cartesian3.fromDegrees(info.lon, info.lat, 0)
    this.addMeshPlane(viewer, position);

    HelperLib.getAxex({
      viewer: viewer,
      type: Libora.AxesHelperType.GLOBE,
    })
  }

  /**
   * 添加Mesh平面
   */
  addMeshPlane(viewer, position) {
    // 方向光
    const light = new Libora.DirectionalLight({
      color: "#ffffff",
      direction: new Cesium.Cartesian3(1, 0, 10),
      viewer: viewer,
    });

    const geometry = new Libora.MeshGeometry({
      type: Libora.MeshGeometryType.PLANE,
      width: 400000,
      height: 100000,
      widthSegments: 1,
      heightSegments: 1,
    });

    const material = new Libora.MeshMaterial({
      type: Libora.MeshMaterialType.BASIC,
      side: Cesium.CullFace.FRONT_AND_BACK,
      color: "#00ff00",
      alpha: 0.1,
      translucent: false,
      directionalLight: light
    })

    const plane = new Libora.Mesh(geometry, material)
    plane.position = position;
    viewer.scene.primitives.add(plane);
    window.aaa = plane;
    HelperLib.getAxex( {
      viewer: viewer,
      target: plane,
      coordinateSystem: Libora.CoordinateSystem.ECEF,
      type: Libora.AxesHelperType.LOCAL,
      scale: 1
    })

    // this.changeOpacity(plane);
  }

  /**
   * 动态改变透明度
   * @param {*} plane
   */
  changeOpacity(plane) {
    let alpha = 0;
    let t = 0;
    setInterval(() => {
      t += 0.01;
      alpha = (Math.sin(t) + 1) / 2;
      viewer.scene.requestRender()
      plane.material.setOptions("alpha", alpha)
    }, 10);
  }

  changeLight(light) {
    let alpha = 0;
    let t = 0;
    setInterval(() => {
      t += 0.01;
      alpha = (Math.sin(t) + 1) / 2;
      viewer.scene.requestRender()
      plane.material.setOptions("alpha", alpha)
    }, 10);
  }
}
